package com.lagou.testDBUtils;

import com.lagou.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * JDBC_PrepareStatement测试类
 */
public class JDBC_PrepareStatement {

    public static void main(String[] args) throws SQLException {

        //1.获取连接
        Connection con = DBUtils.getConnection();

        //2.编写sql语句
        String sql = "select * from Employee where name = ? and gender = ?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);

        //3.设置占位符
        preparedStatement.setString(1,"张百万");
        preparedStatement.setString(2,"男");

        //4.处理结果集
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String gender = resultSet.getString("gender");
            double salary = resultSet.getDouble("salary");
            double bonus = resultSet.getDouble("bonus");
            Date join_date = resultSet.getDate("join_date");

            System.out.println(id + ":" + name + ":" + gender + ":" + salary + ":" + bonus + ":" + join_date);
        }

        //5.释放资源
        DBUtils.close(con,preparedStatement,resultSet);


    }
}
